import User from '../../model/user'
import { createStoreBindings } from 'mobx-miniprogram-bindings'
import { timStore } from '../../store/tim'
// pages/login/login.js
Page({
	/**
	 * 页面的初始数据
	 */
	data: {},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.storeBindings = createStoreBindings(this, {
			store: timStore,
			actions: { timLogin: 'login' },
		})
	},
	onunload() {
		this.storeBindings.destoryStoreBindings()
	},
	async handleLogin() {
		const res = await wx.getUserProfile({
			desc: '完善用户信息',
			mask: true,
		})
		wx.showLoading({
			title: '正在授权',
		})
		try {
			await User.login()
			await User.updateUserInfo(res.userInfo)
			this.timLogin()
			const events = this.getOpenerEventChannel()
			events.emit('login')
			wx.navigateBack()
		} catch (e) {
			wx.showModal({
				title: '注意',
				content: '登陆失败,请稍后重试',
				showCancel: false,
			})
			console.log(e)
		}
		wx.hideLoading()
	},
	handleToHome() {
		wx.switchTab({
			url: '/pages/home/home',
		})
	},
})
